﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Xna.Framework.Media;

namespace MemoBoss
{
    public class SoundHelper
    {
        public static SoundHelper instance = new SoundHelper();
        public string LastSong
        {
            get;
            set;
        }
        public bool IsMusicEnabled
        {
            get;
            set;
        }

        public bool ControlHasBeenAsked
        {
            get;
            set;
        }


        public void PlayMusic(string resourcename, bool ison, bool justensure)
        {
            
            if (resourcename != string.Empty)
            {
                this.LastSong = resourcename;
            }
            this.IsMusicEnabled = ison;
            MediaElement item = App.BGMusicPlayerInstance;
            
            if (ison)
            {
                if (!MediaPlayer.GameHasControl)
                {
                    if (this.ControlHasBeenAsked)
                    {
                        this.IsMusicEnabled = false;
                        return;
                    }
                    else
                    {
                        if (MessageBox.Show("Do you want to stop the media player?", "Stop music?", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel)
                        {
                            this.IsMusicEnabled = false;
                            return;
                        }
                    }
                }
                this.ControlHasBeenAsked = true;
                if (item.Source == null)
                {
                    item.MediaEnded += OnMediaEnded;
                }
                if (!(item.Source != null) || !justensure)
                {
                    if (ison)
                    {
                        //item.Volume = 0.8;
                        item.AutoPlay = true;
                        if (resourcename != string.Empty)
                        {
                            item.Source = new Uri(resourcename, UriKind.RelativeOrAbsolute);
                        }
                        else
                        {
                            item.Source = new Uri(this.LastSong, UriKind.RelativeOrAbsolute);
                        }
                        item.Play();
                    }
                    return;
                }
                else
                {
                    return;
                }
            }
            else
            {
                this.ControlHasBeenAsked = false;
                if (item.Source != null)
                {
                    item.Stop();
                    item.Source = null;
                    item.MediaEnded -= OnMediaEnded;
                }
                return;
            }
        }
        private void OnMediaEnded(object sender, RoutedEventArgs e)
        {
            if (this.IsMusicEnabled)
            {
                this.PlayMusic(this.LastSong, this.IsMusicEnabled,false);
                return;
            }
            else
            {
                return;
            }
        }

    }
}
